package com.baizhi.service.impl;

import com.baizhi.dao.CmfzResourceDao;
import com.baizhi.dao.CmfzRoleDao;
import com.baizhi.dao.CmfzRoleResourceDao;
import com.baizhi.entity.CmfzResource;
import com.baizhi.entity.CmfzRole;
import com.baizhi.entity.CmfzRoleResource;
import com.baizhi.service.CmfzRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/20 0020.
 */
@Service
public class CmfzRoleServiceImpl implements CmfzRoleService {
    @Autowired
    private CmfzRoleDao cmfzRoleDao;
    @Autowired
    private CmfzRoleResourceDao cmfzRoleResourceDao;
    @Autowired
    private CmfzResourceDao cmfzResourceDao;


    @Override
    public Map queryAll(Integer page, Integer rows) {
        Map map = new HashMap();

        IPage<CmfzRole> iPage=new Page<>(page,rows);
        IPage<CmfzRole> cmfzRoleIPage = cmfzRoleDao.selectPage(iPage, null);
        List<CmfzRole> cmfzRoles = cmfzRoleIPage.getRecords();

        for(CmfzRole cmfzRole:cmfzRoles){
            String roleName = cmfzRole.getRoleName();

            /*查询中间表,获取资源信息*/
            List<CmfzRoleResource> cmfzRoleResources = cmfzRoleResourceDao.selectList(new QueryWrapper<CmfzRoleResource>().eq("role_name", roleName));
            ArrayList<Integer> resourceIds = new ArrayList<>();
            for(CmfzRoleResource cmfzRoleResource:cmfzRoleResources){
                resourceIds.add(cmfzRoleResource.getResourceId());
            }
             /*查询resource表，获取资源信息*/
            List<CmfzResource> cmfzResources = cmfzResourceDao.selectList(new QueryWrapper<CmfzResource>().in("resource_id", resourceIds));
            cmfzRole.setCmfzResourceList(cmfzResources);
        }
        long total = cmfzRoleIPage.getTotal();

        map.put("total", total);
        map.put("rows", cmfzRoles);

        return map;
    }
}
